(function(){
	var Model = function(canvas){
		var me = this;
		me.clearRows = 20;		//最大消除行数
		me.clearedRows = 0;		//已经消除的行数
		me.canvas = canvas;
		me.context = canvas.getContext('2d');
		me.blackArr = [];
		me.isGameOn = false;
	}
	
	/**
	 * 游戏计时
	 */
	Model.prototype.start = function(){
		var me = this;
		me.startTime = new Date();		//new Date()表示返回当前时间
		me.lerpTime = 0;
		me.interval = setInterval(function(){
			var time1 = new Date();
			var d = time1 - me.startTime;
			document.querySelector("#divTimer").innerHTML = (d + me.lerpTime) / 1000;
		}, 100);
	}

	/**
	 * 按下开始按钮
	 */
	Model.prototype.gameStart = function(){
		var me = this;
		//初始化操作
		if(me.isGameOn === true){
			var tmpTime = new Date();
			me.lerpTime += tmpTime - me.startTime;
			console.log(me.lerpTime);
			clearInterval(me.interval);
			alert("游戏暂停中…点击确定继续");
			me.startTime = new Date();
			me.interval = setInterval(function(){
				var time1 = new Date();
				var d = time1 - me.startTime;
				document.querySelector("#divTimer").innerHTML = (d + me.lerpTime) / 1000;
			}, 100);
			return;
		}
		else{
			me.isGameOn = true;
			document.querySelector("#bt").innerHTML = "暂停";
		}
		me.clearedRows = 0;
		clearInterval(me.interval);				//清空计时器
		me.context.clearRect(0, 0, me.canvas.width, me.canvas.height);	//清扫画布
		//初始化列索引
		me.blackArr.length = 0;
		for(var i = 0; i < 6; i++){
			var index = Math.random() * 4 | 0;//*4取0-3.  |0 取整
			me.blackArr[i] = index;
		}
		//console.log("绘制前的:" + me.blackArr[5]);
		me.drawGrid();
		me.drawBlock();
		me.start();
		//给canvas添加鼠标点击事件
		me.canvas.addEventListener("click", function(e){
			if(me.isGameOn === false)	return;
			var x = e.offsetX;
			var y = e.offsetY;
			//console.log("判断是否为黑白时的:" + me.blackArr[5]);
			var r = me.checkBlock(x, y);
			//console.log("检查函数返回的值:" + r);
			if(r === 0){
				alert("Game Over!");
				clearInterval(me.interval);	//停止计时器
				me.isGameOn = false;
				location.reload(true);
				return;
			}
			else if(r === 2){
				if(++me.clearedRows === me.clearRows){
					alert("踩完了");
					clearInterval(me.interval);	//停止计时器
					me.isGameOn = false;
					document.querySelector("#bt").innerHTML = "游戏开始";
					me.context.clearRect(0, 0, me.canvas.width, me.canvas.height);	//清扫画布
					me.blackArr.pop();
					me.drawGrid();
					me.drawBlock();
					location.reload(true);
					return;
				}
				me.context.clearRect(0, 0, me.canvas.width, me.canvas.height);	//清扫画布
				me.blackArr.pop();
				if(me.clearRows - me.clearedRows >=  6){
					me.blackArr.unshift(Math.random() * 4 | 0);
				}
				else{
					me.blackArr.unshift(-1);
				}
				me.drawGrid();
				me.drawBlock();
			}
		});
	}

	/**
	 * 绘制格子
	 */
	Model.prototype.drawGrid = function(){
		var me = this;
		var ctx = me.context;
		var height = me.canvas.height / 6;
		var width = me.canvas.width / 4;
		
		//绘制横线
		for(var i = 0; i < 5; i++){
			ctx.beginPath();
			ctx.fillStyle = '#000000';
			var y = (i + 1) * height;
			ctx.moveTo(0, y);
			ctx.lineTo(me.canvas.width, y);
			ctx.stroke();
			ctx.closePath();
		}
		
		//绘制竖线
		for(var i = 0; i < 3; i++){
			ctx.beginPath();
			ctx.fillStyle = '#000000';
			var x = (i + 1) * width;
			ctx.moveTo(x, 0);
			ctx.lineTo(x, me.canvas.height);
			ctx.stroke();
			ctx.closePath();
		}
	}
	
	/**
	 * 绘制黑块
	 */
	Model.prototype.drawBlock = function(){
		var me = this;
		var ctx = me.context;
		var width = me.canvas.width / 4;
		
		//设置绘制颜色
		ctx.fillStyle = '#000000';

		for(var i = 0; i < 6; i++){
			if(me.blackArr[i] === -1)	continue;
			var y = i * width;
			var x = me.blackArr[i] * width;
			ctx.fillRect(x, y, width, width);
		}
	}
	
	/**
	 * 检查是否为黑块
	 * @param x	点击的x坐标
	 * @param y	点击的y坐标
	 * @return 0	点击的是白块
	 * @return 1	点击的是黑块（非最后一行）
	 * @return 2	点击的是黑块（最后一行）
	 */
	Model.prototype.checkBlock = function(x, y){
		var me = this;
		var width = me.canvas.width / 4;
		var col = x/width | 0;
		var row = y/width | 0;
		//console.log("我按的位置:" + row + ',' + col);
		//console.log("黑块的位置:" + row + ',' + me.blackArr[row]);
		if(me.blackArr[row] !== col){
			return 0;
		}
		else{
			if(row === me.blackArr.length - 1){
				return 2;
			}
			else return 1;
		}//三个等号表示恒等，不代表数字的转换  ==数据会发生转换
	}
	//暴露WhiteBlock
	window.WhiteBlock = Model;
}());